---
title: "Elasticsearch DSL"
date: 2019-9-18
categories:
- elasticsearch
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org09bc01c">Full Text Query 全文查询</a>
<ul>
<li><a href="#org2bd902f">intervals query</a></li>
<li><a href="#org0740f52">match query</a></li>
</ul>
</li>
<li><a href="#org5ab2393">Examples</a>
<ul>
<li><a href="#orgfac1941">Or</a></li>
<li><a href="#org3760ee2">Nested</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-org09bc01c">
<h2 id="org09bc01c">Full Text Query 全文查询</h2>
<div class="outline-text-2" id="text-org09bc01c">
<p>
能够搜索分词后的字段（such as the body of an email）。查询字符串被这个字段设置的分词器处理。
这组查询包含：
</p>
</div>
<div class="outline-3" id="outline-container-org2bd902f">
<h3 id="org2bd902f">intervals query</h3>
</div>
<div class="outline-3" id="outline-container-org0740f52">
<h3 id="org0740f52">match query</h3>
</div>
</div>
<div class="outline-2" id="outline-container-org5ab2393">
<h2 id="org5ab2393">Examples</h2>
<div class="outline-text-2" id="text-org5ab2393">
</div>
<div class="outline-3" id="outline-container-orgfac1941">
<h3 id="orgfac1941">Or</h3>
<div class="outline-text-3" id="text-orgfac1941">
<p>
'should' 相当于or，should内的条件需要匹配一个。相对的must，must中的条件需要全部匹配
Where spu like '%23%' or info match '新增' or barCode like '%81%'
</p>
<div class="org-src-container">
<pre class="src src-js">{
    <span style="font-style: italic;">"query"</span>:{
        <span style="font-style: italic;">"bool"</span>: {
            <span style="font-style: italic;">"should"</span>: [
                {<span style="font-style: italic;">"wildcard"</span>: {<span style="font-style: italic;">"spu"</span>: <span style="font-style: italic;">"*23*"</span>}},
                {<span style="font-style: italic;">"match"</span>: {<span style="font-style: italic;">"info"</span>: <span style="font-style: italic;">"新增"</span>}},
                {<span style="font-style: italic;">"wildcard"</span>: {<span style="font-style: italic;">"barCode"</span>: <span style="font-style: italic;">"*81*"</span>}}
            ]
        }
    }
}
</pre>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-org3760ee2">
<h3 id="org3760ee2">Nested</h3>
<div class="outline-text-3" id="text-org3760ee2">
<p>
Where attributes.attrName like '%颜色%'
</p>
<p>
下面用于查询查询嵌套的文档，attributes是文档中子对象：
</p>
<div class="org-src-container">
<pre class="src src-js">{
  <span style="font-style: italic;">"title"</span>: <span style="font-style: italic;">"xxx"</span>,
  <span style="font-style: italic;">"attributes"</span>: [{
    <span style="font-style: italic;">"attrName"</span>: <span style="font-style: italic;">"颜色"</span>,
    <span style="font-style: italic;">"attrValue"</span>: <span style="font-style: italic;">"红色"</span>
  }]
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-js">{
    <span style="font-style: italic;">"query"</span>:{
        <span style="font-style: italic;">"bool"</span>: {
            <span style="font-style: italic;">"should"</span>: [{
                <span style="font-style: italic;">"nested"</span>: {
                    <span style="font-style: italic;">"path"</span>: <span style="font-style: italic;">"attributes"</span>,
                    <span style="font-style: italic;">"query"</span>: {
                        <span style="font-style: italic;">"bool"</span>: {
                            <span style="font-style: italic;">"must"</span>: [
                                {<span style="font-style: italic;">"match"</span>:{<span style="font-style: italic;">"attributes.attrName"</span>: <span style="font-style: italic;">"*颜色*"</span>}}
                            ]
                        }
                    }
                }
            }
                      ]
        }
    }
}
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2019-9-18</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
